<?php
error_reporting(E_ERROR);
const BASE_URL = 'sushi.tv';
const USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36';
const HEADER_HOST = ["Host: " . BASE_URL,
					"Connection: keep-alive",
					"Upgrade-Insecure-Requests: 1",
					"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
					"Accept-Encoding: gzip, deflate, sdch",
					"Accept-Language: en-US,en;q=0.8"];

$url = 'http://' . BASE_URL . '/';


$res = curlWithCookie($url);
if (empty($res)) {
	$res = getHtml($url);
}
$doc = new DOMDocument;

$doc->loadHTML($res);

$xpath = new DOMXPath($doc);

$elements = $xpath->query('/html/head/title');

//未登录
if(preg_match('/Log\s+in/i', $elements[0]->nodeValue)) {
	$res = getHtml($url);

	$doc = new DOMDocument;

	$doc->loadHTML($res);

	$xpath = new DOMXPath($doc);

	//获取csrf的node
	$elements = $xpath->query('//*[@id="loginForm"]/input[1]');

	$csrf = $elements[0]->getAttribute('value');

	$res = login($csrf);

	if (!$res) {
		echo "login error";echo PHP_EOL;
		exit;
	}

	$res = curlWithCookie($url);
}
file_put_contents('./a.html', $res);
echo "success";










function login($csrf) {
	$login_url = 'https://passport.'. BASE_URL .'/login';
	$post_fields = 'email=mo@gladmo.com&password=nnqi15267873387&rememberMe=1&_csrf=' . $csrf . '&r=http://' . BASE_URL . '/&_csrf=' . $csrf;
	$ch = curl_init($login_url);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
	curl_setopt($ch,CURLOPT_HTTPHEADER, HEADER_HOST);
    curl_setopt($ch,CURLOPT_USERAGENT, USER_AGENT);
    curl_setopt($ch,CURLOPT_CAINFO ,"./cacert.pem");
	curl_setopt($ch, CURLOPT_COOKIEFILE, './temp/first.temp');
	curl_setopt($ch, CURLOPT_COOKIEJAR, './temp/cookie.temp');
	$res = curl_exec($ch);
	if($res === false) {
	    echo 'Curl error: ' . curl_error($ch);
	}
	file_put_contents('./b.html', gzdecode($res));
	curl_close($ch);
	sleep(1);
	return gzdecode($res);
}

function curlWithCookie($url) {

	$ch = curl_init($url);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch,CURLOPT_USERAGENT, USER_AGENT);
    curl_setopt($ch,CURLOPT_HTTPHEADER, HEADER_HOST);
    curl_setopt($ch,CURLOPT_CAINFO ,"cacert.pem");
	curl_setopt($ch, CURLOPT_COOKIEFILE, './temp/cookie.temp');
	$res = curl_exec($ch);
	if($res === false) {
	    echo 'Curl error: ' . curl_error($ch);
	}
	curl_close($ch);
	sleep(1);
	return gzdecode($res);
}

function getHtml($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch,CURLOPT_USERAGENT, USER_AGENT);
    curl_setopt($ch,CURLOPT_HTTPHEADER, HEADER_HOST);
    curl_setopt($ch,CURLOPT_CAINFO ,"cacert.pem");
	curl_setopt($ch, CURLOPT_COOKIEJAR, './temp/first.temp');
    $res = curl_exec($ch);
	if($res === false) {
	    echo 'Curl error: ' . curl_error($ch);
	}
    curl_close($ch);
    sleep(1);
    return gzdecode($res);
}
